const Base = require('./base/base'),
	fs = require('fs');

/**
 * @extends base
 */
module.exports = class extends Base {
	encodeAction(param){
		var t;
		var b = [
			"(c^_^o)",
			"(ﾟΘﾟ)",
			"((o^_^o) - (ﾟΘﾟ))",
			"(o^_^o)",
			"(ﾟｰﾟ)",
			"((ﾟｰﾟ) + (ﾟΘﾟ))",
			"((o^_^o) +(o^_^o))",
			"((ﾟｰﾟ) + (o^_^o))",
			"((ﾟｰﾟ) + (ﾟｰﾟ))",
			"((ﾟｰﾟ) + (ﾟｰﾟ) + (ﾟΘﾟ))",
			"(ﾟДﾟ) .ﾟωﾟﾉ",
			"(ﾟДﾟ) .ﾟΘﾟﾉ",
			"(ﾟДﾟ) ['c']",
			"(ﾟДﾟ) .ﾟｰﾟﾉ",
			"(ﾟДﾟ) .ﾟДﾟﾉ",
			"(ﾟДﾟ) [ﾟΘﾟ]"
			];
		var r = "ﾟωﾟﾉ= /｀ｍ´）ﾉ ~┻━┻   //*´∇｀*/ ['_']; o=(ﾟｰﾟ)  =_=3; c=(ﾟΘﾟ) =(ﾟｰﾟ)-(ﾟｰﾟ); "; 
		
		if( /ひだまりスケッチ×(365|３５６)\s*来週も見てくださいね[!！]/.test( param.text ) ){
			r += "X=_=3; ";
			r += "\r\n\r\n    X / _ / X < \"来週も見てくださいね!\";\r\n\r\n";
		}
		r += "(ﾟДﾟ) =(ﾟΘﾟ)= (o^_^o)/ (o^_^o);"+
			"(ﾟДﾟ)={ﾟΘﾟ: '_' ,ﾟωﾟﾉ : ((ﾟωﾟﾉ==3) +'_') [ﾟΘﾟ] "+
			",ﾟｰﾟﾉ :(ﾟωﾟﾉ+ '_')[o^_^o -(ﾟΘﾟ)] "+
			",ﾟДﾟﾉ:((ﾟｰﾟ==3) +'_')[ﾟｰﾟ] }; (ﾟДﾟ) [ﾟΘﾟ] =((ﾟωﾟﾉ==3) +'_') [c^_^o];"+
			"(ﾟДﾟ) ['c'] = ((ﾟДﾟ)+'_') [ (ﾟｰﾟ)+(ﾟｰﾟ)-(ﾟΘﾟ) ];"+
			"(ﾟДﾟ) ['o'] = ((ﾟДﾟ)+'_') [ﾟΘﾟ];"+
			"(ﾟoﾟ)=(ﾟДﾟ) ['c']+(ﾟДﾟ) ['o']+(ﾟωﾟﾉ +'_')[ﾟΘﾟ]+ ((ﾟωﾟﾉ==3) +'_') [ﾟｰﾟ] + "+
			"((ﾟДﾟ) +'_') [(ﾟｰﾟ)+(ﾟｰﾟ)]+ ((ﾟｰﾟ==3) +'_') [ﾟΘﾟ]+"+
			"((ﾟｰﾟ==3) +'_') [(ﾟｰﾟ) - (ﾟΘﾟ)]+(ﾟДﾟ) ['c']+"+
			"((ﾟДﾟ)+'_') [(ﾟｰﾟ)+(ﾟｰﾟ)]+ (ﾟДﾟ) ['o']+"+
			"((ﾟｰﾟ==3) +'_') [ﾟΘﾟ];(ﾟДﾟ) ['_'] =(o^_^o) [ﾟoﾟ] [ﾟoﾟ];"+
			"(ﾟεﾟ)=((ﾟｰﾟ==3) +'_') [ﾟΘﾟ]+ (ﾟДﾟ) .ﾟДﾟﾉ+"+
			"((ﾟДﾟ)+'_') [(ﾟｰﾟ) + (ﾟｰﾟ)]+((ﾟｰﾟ==3) +'_') [o^_^o -ﾟΘﾟ]+"+
			"((ﾟｰﾟ==3) +'_') [ﾟΘﾟ]+ (ﾟωﾟﾉ +'_') [ﾟΘﾟ]; "+
			"(ﾟｰﾟ)+=(ﾟΘﾟ); (ﾟДﾟ)[ﾟεﾟ]='\\\\'; "+
			"(ﾟДﾟ).ﾟΘﾟﾉ=(ﾟДﾟ+ ﾟｰﾟ)[o^_^o -(ﾟΘﾟ)];"+ 
			"(oﾟｰﾟo)=(ﾟωﾟﾉ +'_')[c^_^o];"+//TODO
			"(ﾟДﾟ) [ﾟoﾟ]='\\\"';"+ 
			"(ﾟДﾟ) ['_'] ( (ﾟДﾟ) ['_'] (ﾟεﾟ+";
		r += "(ﾟДﾟ)[ﾟoﾟ]+ ";
		for( var i = 0; i < param.text.length; i++ ){
			let n = param.text.charCodeAt( i );
			let t = "(ﾟДﾟ)[ﾟεﾟ]+";
			if( n <= 127 ){
				t += n.toString( 8 ).replace( /[0-7]/g, function(c){ return b[ c ] + "+ "; } );
			}else{
				var m = /[0-9a-f]{4}$/.exec( "000" + n.toString(16 ) )[0];
				t += "(oﾟｰﾟo)+ " + m.replace( /[0-9a-f]/gi, function(c){ return b[ parseInt( c,16 ) ] + "+ "; } );
			}
			r += t;

		}
		r += "(ﾟДﾟ)[ﾟoﾟ]) (ﾟΘﾟ)) ('_');";
		return r;
	}
	decodeAction(param){
		let b = [
		  /\(c\^_\^o\)/,
		  /\(ﾟΘﾟ\)/,
		  /\((o\^_\^o\) \- \(ﾟΘﾟ\))/,
		  /\(o\^_\^o\)/,
		  /\(ﾟｰﾟ\)/,
		  /\(\(ﾟｰﾟ\) \+ \(ﾟΘﾟ\)\)/,
		  /\(\(o\^_\^o\) \+\(o\^_\^o\)\)/,
		  /\(\(ﾟｰﾟ\) \+ \(o\^_\^o\)\)/
		];
		let text = (param.text.slice(0,1) === '@')?fs.readFileSync(param.text.slice(1)).toString():param.text;
		let value = text.replace(/(.*?)\(ﾟДﾟ\)\[ﾟoﾟ\]\+[ ]*/,'')
			.replace(/\+[ ]*\(ﾟДﾟ\)\[ﾟoﾟ\](.*)/,'')
			.replace(/\(ﾟДﾟ\)\[ﾟεﾟ\]\+/g,'\r\n')
			.replace(new RegExp(b[7],"gm"),"7")
			.replace(new RegExp(b[6],"gm"),"6")
			.replace(new RegExp(b[5],"gm"),"5")
			.replace(new RegExp(b[2],"gm"),"2")
			.replace(new RegExp(b[4],"g"),"4")
			.replace(new RegExp(b[3],"g"),"3")
			.replace(new RegExp(b[1],"g"),"1")
			.replace(new RegExp(b[0],"g"),"0")
			.replace(/[\(\)\+ ]?/g,'')
			.trim()
			.replace(/\r\n/g,',')
			.split(',')
			.map(function(c){return String.fromCharCode(parseInt('0'+c,8))})
			.join('');

		return value;
	}
}
